import 'package:flutter/services.dart';

class UnicomMethodChannel {
  final String channelName;
  MethodChannel channel;

  UnicomMethodChannel(this.channelName) : channel = MethodChannel(channelName);

  Future callNative(String methodName, dynamic args) {
    // 判断是否是生产环境，生产环境不打印 Log
    // ignore: prefer_const_constructors
    if (bool.fromEnvironment("dart.vm.product")) {
      return channel.invokeMethod(methodName, args);
    } else {
      // ignore: avoid_print
      print("\n---------------- flutter call native ----------------\n\nmethod_name: $methodName\nparameters: ${args.toString()}"
          "\n\n-----------------------------------------------------");
      return channel.invokeMethod(methodName, args).then((value) {
        // ignore: avoid_print
        print("\n---------------- response from native ---------------\n\nmethod_name: $methodName\nresponse: ${value.toString()}"
            "\n\n-----------------------------------------------------");
        return value;
      });
    }
  }

  // ignore: use_function_type_syntax_for_parameters
  setListener(Future<dynamic> handler(MethodCall call)) {
    channel.setMethodCallHandler(handler);
  }
}
